package com.tencent.bugly.beta.global;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.tencent.bugly.beta.download.DownloadTask;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.p;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class f {
    public static f a = new f();
    private List<Runnable> d = new ArrayList();
    final Map<String, DownloadTask> b = Collections.synchronizedMap(new HashMap());
    Handler c = new Handler(Looper.getMainLooper());

    public synchronized void a(Runnable runnable, int i) {
        if (this.b.size() == 0) {
            runnable.run();
        } else {
            d dVar = new d(6, false, runnable);
            this.c.postDelayed(dVar, i);
            a(dVar);
        }
    }

    public synchronized void a(Runnable runnable) {
        if (this.b.size() == 0) {
            runnable.run();
        } else {
            this.d.add(runnable);
        }
    }

    public void a(com.tencent.bugly.beta.download.b bVar, Map<String, String> map) {
        String[] strArr;
        DownloadTask downloadTask;
        if (bVar == null) {
            return;
        }
        if (map == null || map.isEmpty()) {
            this.b.clear();
            ResBean.a = new ResBean();
            a.a("rb.bch", ResBean.a);
            return;
        }
        for (DownloadTask downloadTask2 : this.b.values()) {
            downloadTask2.delete(true);
        }
        this.b.clear();
        com.tencent.bugly.beta.download.a aVar = new com.tencent.bugly.beta.download.a(1, this, this.b);
        for (String str : ResBean.b) {
            if (!map.containsKey(str)) {
                this.b.clear();
                ResBean.a = new ResBean();
                a.a("rb.bch", ResBean.a);
                return;
            }
            String str2 = map.get(str);
            if (!str.startsWith("IMG_") || TextUtils.isEmpty(str2)) {
                ResBean.a.a(str, str2);
            } else {
                ResBean.a.a(str, "");
                Iterator<DownloadTask> it = this.b.values().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        downloadTask = null;
                        break;
                    }
                    downloadTask = it.next();
                    if (downloadTask.getDownloadUrl().equals(str2)) {
                        break;
                    }
                }
                if (downloadTask == null) {
                    downloadTask = bVar.a(str2, e.E.r.getAbsolutePath(), null, null);
                }
                if (downloadTask != null) {
                    downloadTask.addListener(aVar);
                    downloadTask.setNeededNotify(false);
                    this.b.put(str, downloadTask);
                }
            }
        }
        a.a("rb.bch", ResBean.a);
        if (this.b.isEmpty()) {
            return;
        }
        for (DownloadTask downloadTask3 : this.b.values()) {
            downloadTask3.download();
        }
    }

    public void a() {
        String[] strArr;
        ArrayList<String> arrayList = new ArrayList();
        for (String str : ResBean.b) {
            String a2 = ResBean.a.a(str);
            if (str.startsWith("IMG_") && !TextUtils.isEmpty(a2)) {
                arrayList.add(a2);
            }
        }
        File[] listFiles = e.E.r.listFiles();
        if (listFiles == null || listFiles.length <= 0) {
            return;
        }
        for (File file : listFiles) {
            boolean z = true;
            for (String str2 : arrayList) {
                if (file.getAbsolutePath().equals(str2)) {
                    z = false;
                }
            }
            if (z) {
                p.a.b(file.getAbsolutePath());
                if (!file.delete()) {
                    an.e("cannot deleteCache file:%s", file.getAbsolutePath());
                }
            }
        }
    }

    public void b() {
        synchronized (this) {
            for (Runnable runnable : this.d) {
                runnable.run();
            }
            for (DownloadTask downloadTask : this.b.values()) {
                downloadTask.delete(false);
            }
            this.d.clear();
            this.b.clear();
        }
    }
}
